package cnpm.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;

import cnpm.dao.OfficeDAO;
import cnpm.entity.Office;
import cnpm.util.HibernateUtil;

public class OfficeDAOImpl implements OfficeDAO {

	private SessionFactory sessionFactory;
	private Session session;
	
	public OfficeDAOImpl() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Office> getAllOffice() {
		List<Office> list=null;
		try{
			session = sessionFactory.openSession();
			list = (List<Office>) session.createCriteria(Office.class)
					.addOrder(Order.asc("officeName"))
					.list();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			session.close();
		}
		return list;
	}

	@Override
	public Office getOffice(int officeID) {
		try{
			session = sessionFactory.openSession();
			return (Office) session.get(Office.class, officeID);
		}catch(HibernateException e){
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return null;
	}

}
